package Gui.Dialogs;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.FontMetrics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import Gui.GameFrame;
import Gui.GamePanel;
import Main.Defines;
import javax.swing.JLabel;

public class ViewLoansDialog extends JDialog {
	/**
	 * 
	 */
	private static final long serialVersionUID = -4698489721588997308L;
	private JPanel jp;
	private JLabel moneyInfo;
	private int originalCash;
	private int originalLoan;
	private JPanel labels;
	private JPanel moneyModifiersPanel;
	private JPanel confirmButtonsPanel;

	private void buildUIContents() {
		originalCash = Defines.CP().getCash();
		originalLoan = Defines.CP().getLoan();

		labels = new JPanel(new FlowLayout());
		moneyModifiersPanel = new JPanel();
		confirmButtonsPanel = new JPanel(new FlowLayout());

		moneyInfo = new JLabel(String.format(
				"<html>Current Loan Amount: $%d<br>Player %d Cash: $%d",
				Defines.CP().getLoan(), Defines.CP().getIDPrintable(), Defines
						.CP().getCash()));
		JButton loan = new JButton(String.format(
				"<html>Loan $%d<br> from Bank", Defines.LOAN_AMOUNT));
		JButton payback = new JButton(String.format(
				"<html>Payback $%d<br> to Bank", Defines.LOAN_AMOUNT));
		JButton ok = new JButton("OK");
		JButton cancel = new JButton("Cancel");
		// add each component to its designated panel
		labels.add(moneyInfo);
		moneyModifiersPanel.add(loan);
		moneyModifiersPanel.add(payback);
		confirmButtonsPanel.add(ok);
		confirmButtonsPanel.add(cancel);

		// set action listeners
		loan.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				Defines.CP().AddLoan(Defines.LOAN_AMOUNT);
				Defines.CP().AddCash(Defines.LOAN_AMOUNT);
				update();
			}
		});
		payback.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO check if player can afford payback
				// also check if player owes at least loan amount

				if (Defines.CP().getCash() >= Defines.LOAN_AMOUNT
						&& Defines.CP().getLoan() >= Defines.LOAN_AMOUNT) {
					Defines.CP().AddLoan(-Defines.LOAN_AMOUNT);
					Defines.CP().AddCash(-Defines.LOAN_AMOUNT);
				}
				update();

			}
		});
		ok.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {

				// TODO setChangeInCash for loan amount
				dispose();
				GameFrame.getInstance().update();
			}
		});

		cancel.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				Defines.CP().setCash(originalCash);
				Defines.CP().setLoan(originalLoan);
				GamePanel.getInstance().update();
				dispose();
				GameFrame.getInstance().update();
			}
		});

		// add panels to jp
		jp.add(labels, BorderLayout.NORTH);
		jp.add(moneyModifiersPanel, BorderLayout.CENTER);
		jp.add(confirmButtonsPanel, BorderLayout.SOUTH);
	}

	public ViewLoansDialog() {
		setModalityType(getModalityType().APPLICATION_MODAL);
		jp = new JPanel();
		jp.setLayout(new BorderLayout()); 
		

		buildUIContents();
		add(jp);
		pack();
		
		if (getTitle() != null) {
			FontMetrics fm = getFontMetrics(getFont());
			int width = fm.stringWidth(getTitle()) + Defines.ICON_WIDTH; 
			width = (int) Math.max(width * 1.2, getPreferredSize().getWidth());
			setSize(new Dimension(width, getHeight()));
		}
		setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
		setLocationRelativeTo(GameFrame.getInstance());
		setVisible(true);
		
	}

	private void update() {
		GameFrame.getInstance().update();
		moneyInfo.setText(String.format(
				"<html>Current Loan Amount: $%d<br>Player %d Cash: $%d",
				Defines.CP().getLoan(), Defines.CP().getIDPrintable(), Defines
						.CP().getCash()));

	}
}
